package cn.okcmd.base;

/**
 * create by caoqg on 2021/7/17
 * 功能说明: 测试获取程序执行的行号
 * 应用案例：
 * 1、日志打印行号
 * 2、spring-boot 获取启动类
 */
public class TestNowLineNum {
    public static void main(String[] args) {
        testLineNum1();
        System.out.println();
        testLineNum2();
    }

    private static void testLineNum1(){
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            StackTraceElement ste = stackTrace[i];
            String className = ste.getClassName();
            String methodName = ste.getMethodName();
            int lineNumber = ste.getLineNumber();
            System.out.println("clazz:"+className+", method:"+methodName+", line:"+lineNumber);

        }
    }

    private static void testLineNum2(){
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            StackTraceElement ste = stackTrace[i];
            String className = ste.getClassName();
            String methodName = ste.getMethodName();
            int lineNumber = ste.getLineNumber();
            System.out.println("clazz:"+className+", method:"+methodName+", line:"+lineNumber);

        }
    }
}
